import { ActionContext } from 'vuex'
import { local_storage, session_storage } from '@/utils/storage'
import {
  COMPLAIN_PROMPT_KEY,
  STORE_INFO,
  TOKEN_LOGIN_KEY,
  TOKEN_STORE_KEY,
  UNION_STATUS,
  USER_INFO,
  ORDER_REPORT_MODAL_STATUS,
  ACCOUNT_SETTING_MODAL_STATUS
} from '@/enums/cacheEnum'
import { getUserInfo as getUserInfoApi, login as loginApi, captchaGet, captchaCheck, checkToken } from '@/api/user'
import { IStore } from '../types'
import { LoginParams, CaptchaGetParams, CaptchaCheckParams, SmsCodeParams } from '@/api/model/userModel'

export type IUserInfo = {
  accountId: number
  userName: string
  bindStatus?: number
  accountMobile: string
}
export type IAccountInfo = {
  accountCode: string
  accountName: string
  accountBalance: string | number
}
export type IRefundWarning = {
  open: boolean
  subMich?: number
}

export type IUserState = {
  accountInfo: IAccountInfo
  token: string
  userInfo: IUserInfo
  refundWarning: IRefundWarning
  isManage: boolean
}

const state = {
  token: local_storage.get(TOKEN_LOGIN_KEY) || '',
  userInfo: {
    accountId: null,
    userName: '',
    accountMobile: null
  },
  accountInfo: {
    accountCode: null,
    accountName: null,
    accountBalance: null
  },
  // 退款警告提醒
  refundWarning: {
    open: false,
    subMich: null
  },
  // 是否有投放管理权限
  isManage: false
}

const mutations = {
  SET_TOKEN: (state: IUserState, token: string) => {
    state.token = token
  },
  SET_USERINFO: (state: IUserState, userInfo: IUserInfo) => {
    state.userInfo = userInfo
  },
  SET_REFUND_WARNING: (state: IUserState, refundWarning: IRefundWarning) => {
    state.refundWarning = refundWarning
  },
  SET_IS_MANGER: (state: IUserState, isManage: boolean) => {
    state.isManage = isManage
  },
  SET_ACCOUNT_INFO: (state: IUserState, accountInfo: IAccountInfo) => {
    state.accountInfo = accountInfo
  }
}

const actions = {
  // 登录
  async login({ commit }: ActionContext<IUserState, IStore>, params: LoginParams) {
    console.log(params, 2222)
    if (params.token) {
      // console.log(params.token)
      commit('SET_TOKEN', params.token)
      local_storage.set(TOKEN_LOGIN_KEY, params.token)
      local_storage.remove(TOKEN_STORE_KEY)
      return params.token
    } else {
      const res = await loginApi(params)
      console.log(res, 3333)
      const { data } = res
      commit('SET_TOKEN', data)
      local_storage.set(TOKEN_LOGIN_KEY, data)
      return res
    }
  },
  // user logout
  async logout({ commit }: ActionContext<IUserState, IStore>) {
    // 不需要特意调用接口
    // commit('SET_TOKEN', '')
    // commit('SET_USERINFO', {})
    session_storage.clear() // 清除本地所有session缓存
    local_storage.remove(STORE_INFO)
    local_storage.remove(USER_INFO)
    local_storage.remove(UNION_STATUS)
    local_storage.remove(TOKEN_STORE_KEY)
    local_storage.remove(TOKEN_LOGIN_KEY) // 清除本地token缓存
    local_storage.remove(COMPLAIN_PROMPT_KEY)
    local_storage.remove(ORDER_REPORT_MODAL_STATUS)
    local_storage.remove(ACCOUNT_SETTING_MODAL_STATUS)
    local_storage.remove('LOCAL_ROOT_ROUTER_TYPE')
  },

  // remove token
  async resetToken({ commit }: ActionContext<IUserState, IStore>) {
    commit('SET_TOKEN', '')
    session_storage.clear() // 清除本地所有session缓存
    local_storage.remove(TOKEN_LOGIN_KEY)
    local_storage.remove(TOKEN_STORE_KEY)
  },

  async getUserInfo({ commit }: ActionContext<IUserState, IStore>, params: any) {
    const userInfo = local_storage.get(USER_INFO)
    if (userInfo && !params?.force) {
      commit('SET_USERINFO', userInfo)
      return { data: userInfo }
    } else {
      const res = await getUserInfoApi()
      commit('SET_USERINFO', res.data)
      local_storage.set(USER_INFO, res.data)
      return res
    }
  },
  async getAccountInfo({ commit }: ActionContext<IUserState, IStore>, params: any) {
    commit('SET_ACCOUNT_INFO', params)
  },
  async captchaGet({ commit }: ActionContext<IUserState, IStore>, params: CaptchaGetParams) {
    return await captchaGet(params)
  },
  async captchaCheck({ commit }: ActionContext<IUserState, IStore>, params: CaptchaCheckParams) {
    return await captchaCheck(params)
  },
  async checkToken() {
    return await checkToken()
  },
  async setIsManage({ commit }: ActionContext<IUserState, IStore>, isManage: boolean) {
    commit('SET_IS_MANGER', isManage)
    return isManage
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
